package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.Category;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddCategoryActivity extends Activity {

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.add_category);
		listenAction();
	}

	private void listenAction() {

		final EditText labelEdit = (EditText) findViewById(R.id.edit_category_label);
		final EditText descEdit = (EditText) findViewById(R.id.edit_description);

		View editBt = findViewById(R.id.bt_add_category);
		editBt.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (Utils.checkEditText(getApplicationContext(), labelEdit,
				        descEdit)) {

					Category category = new Category();
					category.setName(labelEdit.getText().toString());
					category.setDescription(descEdit.getText().toString());
					long row = DataManagerFacade.getInstance(v.getContext())
					        .saveCategory(category);
					if (row > 0) {
						Toast.makeText(getApplicationContext(),
						        "Votre categorie a ete ajoute avec succes",
						        Toast.LENGTH_LONG).show();
						finish();
					} else {
						Toast.makeText(getApplicationContext(),
						        "Verifier si le nom n'existe pas deja",
						        Toast.LENGTH_LONG).show();
					}

				}
			}
		});

	}

}
